<script>
import { listUser } from "@/api/system/user";
import dmbForm from "@/components/DmbForm";
import dmbTable from "@/components/LTable/index.vue";
import {
  formBase,
  tableBase,
  cellStyle,
  headerCellStyle,
  tableFormStyle,
  formItemStyle,
} from "./config";
import { mapState } from "vuex";
import dayjs from "dayjs";
import store from "@/store";
const defaultForm = {
  courierNumber: "",
  userName: "",
  type: "",
  companyId: "",
  remarks: "",
};

export default {
  name: "specialdel",
  computed: {
    ...mapState({
      company: (state) => state.custom_dict.company,
    }),
    formItemStyle() {
      return formItemStyle;
    },
    tableFormStyle() {
      return tableFormStyle;
    },
    cellStyle() {
      return cellStyle;
    },
    headerCellStyle() {
      return headerCellStyle;
    },
  },
  components: { dmbTable, dmbForm },
  data() {
    return {
      propsSearchForm: { ...defaultForm },
      rebase: formBase,
      requestConfig: {
        url: "/base/product/selectPackingTrucksVoList",
        method: "get",
        montage: true,
      },
      setRequest: {
        url: "/base/manageSpecialDelivery/updateSpecialDelivery",
        method: "put",
        montage: false,
      },
      addRequest: {
        url: "/base/manageSpecialDelivery/addSpecialDelivery",
        method: "put",
        montage: false,
      },
      selectionArr: [], // 多选框内容
      tableSearchForm: { ...defaultForm },
      listUser: [],
    };
  },
  mounted() {
    listUser()
      .then((res) => {
        this.rebase[5].options = res.rows;
        this.listUser = res.rows;
      })
      .catch((err) => {});
  },
  methods: {
    tableBase() {
      return tableBase;
    },
    // 导出
    async downLoad() {
      if (this.selectionArr.length > 0) {
        const ids = this.selectionArr.map((v) => v.id);
        // 给导出的单条数据赋值companyId
        this.selectionArr[0].companyId = data.id;

        this.download(
          "/base/manageSpecialDelivery/exportSpecialDelivery",
          {
            ids,
          },
          `打包装车_${dayjs().format("YYYY/MM/DD hh:mm:ss")}.xlsx`
        );
      } else {
        this.$message.error("请选择条数据导出");
      }
    },
    // 多选框内容
    selectionChange(data) {
      this.selectionArr = data;
    },
  },
  watch: {
    propsSearchForm: {
      deep: true,
      handler: function (value) {
        this.tableSearchForm = { ...value };
        delete this.tableSearchForm.shelfTime;
        // 日期
        if (!value?.shelfTime?.length) {
          this.tableSearchForm.startTime = "";
          this.tableSearchForm.endTime = "";
        } else if (value?.shelfTime?.length) {
          this.tableSearchForm.startTime = dayjs(value.shelfTime[0]).format(
            "YYYY-MM-DD HH:mm:ss"
          );
          this.tableSearchForm.endTime = dayjs(value.shelfTime[1]).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        }
        // 仓库
        if (value.warehouseId) {
          const obj = store.state.custom_dict.warehouse.find((item) => {
            return item.id == value.warehouseId;
          });
          this.tableSearchForm.warehouseName = obj ? obj.warehouseName : "";
        }
        // 扫描人
        if (value.scanner) {
          const obj = this.listUser.find((item) => {
            return item.userId == value.scanner;
          });
          this.tableSearchForm.scanner = obj ? obj.userName : "";
        }
      },
    },
  },
};
</script>

<template>
  <div>
    <h1>未开发</h1>
    <div class="mb-3">
      <dmb-form
        ref="form"
        :form-items="rebase"
        :model-value="propsSearchForm"
        :item-style="formItemStyle"
     @update="(row) => (propsSearchForm = row)"
      >
        <template v-slot:header="row" class="flex flex-row-reverse">
          <div class="mb-3 flex flex-row-reverse">
            <div>
              <el-button type="success" @click="$refs.table.getListData()"
                >查询</el-button
              >
              <el-button type="primary" @click="downLoad">导出</el-button>
            </div>
          </div>
        </template>
      </dmb-form>
    </div>
    <div>
      <dmb-table
        ref="table"
        :prop-list="tableBase()"
        :is-operate="true"
        :show-index-column="true"
        select-column-type="RADIO"
        :props-search-form="tableSearchForm"
        :req-request="requestConfig"
        :set-request="setRequest"
        :add-request="addRequest"
        select-key="id"
        :cell-style="cellStyle"
        :header-cell-style="headerCellStyle"
        :initialize="false"
        :change-search-form="false"
        :show-footer="true"
        :form-item-style="tableFormStyle"
        @selectionChange="
          (row) => {
            selectionArr = row;
          }
        "
      />
    </div>
  </div>
</template>

<style scoped lang="scss">
</style>
